- 论坛徽章:
- 0
|
- /*
- * SendBuf.h
- *
- * Created on: 2010-8-2
- * Author: zliming
- */
- #ifndef SENDBUF_H_
- #define SENDBUF_H_
- #include <vector>
- using namespace std;
- /*
- * tr1/shared_ptr.h在mingw下怪怪,又不想用boost,头太大
- */
- class SharedBuf {
- public:
- SharedBuf(const char* data);
- SharedBuf(const SharedBuf& o);
- ~SharedBuf();
- void operator=(const SharedBuf& o);
- size_t Size();
- const char* GetBuffer();
- private:
- void ReleaseRef();
- private:
- struct Data {
- char* data;
- int refCount;
- size_t len;
- } *ptr;
- };
- #endif /* SENDBUF_H_ */
复制代码- /*
- * SendBuf.cpp
- *
- * Created on: 2010-8-2
- * Author: zliming
- */
- #include "SendBuf.h"
- #include <string.h>
- SharedBuf::SharedBuf(const char* buf) {
- size_t len = strlen(buf);
- ptr = new Data;
- ptr->len = len + 2;
- ptr->data = new char[ptr->len];
- ptr->data[0] = 0;
- memcpy(ptr->data + 1, buf, len);
- ptr->data[len + 1] = 0xff;
- ptr->refCount = 1;
- }
- SharedBuf::SharedBuf(const SharedBuf& o): ptr(0){
- operator=(o);
- }
- SharedBuf::~SharedBuf(){
- ReleaseRef();
- }
- size_t SharedBuf::Size(){
- if(ptr){
- return ptr->len;
- }
- return 0;
- }
- const char* SharedBuf::GetBuffer(){
- if(ptr){
- return ptr->data;
- }
- return NULL;
- }
- void SharedBuf::operator =(const SharedBuf& o){
- //去掉原引用的内存
- ReleaseRef();
- ptr = o.ptr;
- ptr->refCount ++;
- }
- void SharedBuf::ReleaseRef(){
- if(ptr && --ptr->refCount <= 0){
- delete[] ptr->data;
- delete ptr;
- ptr = 0;
- }
- }
复制代码- #ifndef __CLIENT_H__
- #define __CLIENT_H__
- #include <libZLM/LibZLM.h>
- struct SafeDelete
- {
- template <typename T>
- void operator ()(T& t)
- {
- if(t)
- {
- delete t;
- t = NULL;
- }
- }
- };
- #define SAFE_DELETE SafeDelete()
- class Client
- {
- public:
- virtual void Start() { }
- virtual ~Client() { }
- Socket& GetSocket(){ return _socket; }
- private:
- Socket _socket;
- };
- template<typename C>
- class Server
- {
- public:
- Server(int port) : _listen("0.0.0.0", port) { }
- virtual void Accept(const Error& ec, C* client)
- {
- if(client)
- {
- if(!ec)
- client->Start();
- else
- delete client;
- }
- client = new C;
- _listen.AsyncAccept(&client->GetSocket(), Bind(&Server::Accept, this, _1, client));
- }
- virtual ~Server(){}
- protected:
- Listen _listen;
- };
- #endif//__CLIENT_H__
复制代码- /*
- * websocket.h
- *
- * Created on: 2010-7-29
- * Author: zliming
- */
- #ifndef WEBSOCKET_H_
- #define WEBSOCKET_H_
- #include "Socket.h"
- class WebSocket : public Client {
- public:
- void Start();
- virtual void DoCommand(Record& record);
- private:
- void Do(const Error& err, int len);
- void OnRead(const Error& err, int len);
- private:
- char buf[0x8000];
- size_t read_bytes;
- int step;
- char* buf_offset;
- };
- #endif /* WEBSOCKET_H_ */
复制代码- //============================================================================
- // Name : websocket.cpp
- // Author : zliming
- //============================================================================
- #include "websocket.h"
- #include "Handshaker.h"
- #include <stdlib.h>
- #include <string.h>
- #include <memory.h>
- #include <stdio.h>
- #include <string>
- using namespace std;
- static char flash_policy_req[] = "<policy-file-request/>";
- static char flash_policy_ret[] = "<cross-domain-policy>\n"
- "<allow-access-from domain='*' to-ports='*'/>\n"
- "</cross-domain-policy>";
- static char http_end_flag[] = "\r\n\r\n";
- void WebSocket::Start() {
- step = 1;
- read_bytes = 0;
- Do(Error(), 0);
- }
- void WebSocket::Do(const Error& err, int len) {
- switch (step) {
- case 1:
- step = 2;
- return GetSocket().AsyncRead(buf, sizeof(flash_policy_req), Bind(
- &WebSocket::Do, this, _1, _2));
- case 2:
- //如果是通过Flash桥来连接的,会先读求连接策略
- if (memcmp(flash_policy_req, buf, sizeof(flash_policy_req)) == 0) {
- step = 3;
- return GetSocket().AsyncWrite(flash_policy_ret,
- sizeof(flash_policy_ret),
- Bind(&WebSocket::Do, this, _1, _2));
- case 3:
- return delete this;
- } else {
- case 4:
- read_bytes += len;
- buf[read_bytes] = 0;
- buf_offset = strstr(buf, http_end_flag);
- if (buf_offset) {
- //HTTP头读完,还要读8个字节(key3)
- if (buf + read_bytes - (buf_offset + 4) < 8) {
- step = 5;
- return GetSocket().AsyncRead(buf + read_bytes, 8 - (buf
- + read_bytes - (buf_offset + 4)), Bind(
- &WebSocket::Do, this, _1, _2));
- case 5:
- read_bytes += len;
- buf[read_bytes] = 0;
- }
- //全体需要的数据完成
- Handshaker handshaker(buf, read_bytes);
- const string& result = handshaker.GetResult();
- if(result.empty()){
- return delete this;
- }
- memcpy(buf, result.c_str(), result.length());
- memcpy(buf + result.length(), handshaker.GetChallenge(), 16);
- read_bytes = 0;
- return GetSocket().AsyncWrite(buf, result.length() + 16,
- Bind(&WebSocket::OnRead, this, _1, 0));
- } else {
- step = 4;
- return GetSocket().AsyncReadSome(buf + read_bytes, sizeof(buf)
- - read_bytes - 1, Bind(&WebSocket::Do, this, _1, _2));
- }
- }
- }
- }
- void WebSocket::OnRead(const Error& err, int len){
- if(!err){
- read_bytes += len;
- char* find, *prog = buf;
- while((find = (char*)memchr(prog, 0xff, buf + read_bytes - prog)) != NULL){
- //websocket发送的数据为: 00 data ff
- *find = 0;
- //这个是Json数据的操作类,和javascript通讯,json是不二选择
- Record record;
- if(record.FromString(prog + 1, find - prog - 1)){
- this->DoCommand(record);
- }
- //处理完成,跳过已经处理部分数据
- prog = find + 1;
- }
- if(prog != buf){
- //已经处理完的数据丢弃
- memmove(buf, prog, buf + read_bytes - prog);
- read_bytes = buf + read_bytes - prog;
- }
- if(sizeof(buf) - read_bytes - 1 > 0){
- //读新的数据进来
- return GetSocket().AsyncReadSome(buf + read_bytes,
- sizeof(buf) - read_bytes - 1,
- Bind(&WebSocket::OnRead, this, _1, _2));
- }
- }
- delete this;
- }
- void WebSocket::DoCommand(Record& record){
- }
复制代码 |
|